<template>
	<view class="app-container">
		<view class="title">
			账号登录
		</view>
		<view class="form">
			<view class="item">
				<input v-model="form.username" type="text"  placeholder="登录手机"/>
			</view>
			<view class="item">
				<input v-model="form.password" type="password" placeholder="登录密码"/>
			</view>
		</view>
		<view class="btn">
			<button type="primary" @click="handleLogin">登 录</button>
			<view class="btn-text">
				<view @click="goPage('register')">前往注册</view>
				<view @click="goPage('password')">忘记密码</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {setToken,setUser} from '../../utils/auth.js'
	export default {
		data() {
			return {
				form:{
					username:'',
					password:''
				}
			}
		},
		methods: {
			handleLogin() {
				let username = this.form.username.trim()
				let password = this.form.password.trim()
				if(!username || !password){
					return uni.showToast({
						icon:'none',
						title:'请填写完整'
					})
				}
				const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
				
				if(!reg.test(username)){
					return uni.showToast({
						icon:'none',
						title:'手机格式错误'
					})
				}
				this.$http('/login?username='+username+'&password='+password,'POST').then(res => {
					let token = res.data.token
					setToken(token)
					let jsonData = {
						username:username,
						did:uni.getSystemInfoSync().deviceId
					}
					this.$store.dispatch("getInfo",jsonData).then(res => {
						setUser(res)
						uni.switchTab({
							url:'/pages/message/message'
						})
					})
				})
				
			},
			goPage(url) {
				uni.navigateTo({
					url:`/pages/login/${url}`
				})
			}
		}
	}
</script>

<style>
	.title{
		font-weight: bold;
		font-size: 24px;
		color: #666;
		padding: 10px 20px;
	}

	.item{
		display: flex;
		border-bottom: 2px solid #eee;
		margin: 10px 20px;
		padding: 10px;
	}
	.btn{
		padding: 10px 20px;
	}
	.btn-text{
		display: flex;
		justify-content: center;
	}
	.btn-text view{
		padding: 10px;
		color: #007aff;
	}
</style>
